<?php defined( 'BASEPATH' ) or die( 'Restricted' );

/*
This code is copyright 2009-2010 by TMLA INC.  ALL RIGHTS RESERVED.
Please view license.txt in /tgsf_core/legal/license.txt or
http://tgWebSolutions.com/opensource/tgsf/license.txt
for complete licensing information.
*/


define( 'linkExistsUpdated', '0' );
define( 'linkExistsTooNew', '1' );
define( 'linkNotExists', '2' );
class links_model extends tgsfBase
{
	//------------------------------------------------------------------------
	/**
	*
	*/
	public function getId( $url )
	{
		return query::factory()
			->select( 'link_id' )
			->from( 'links' )
			->where( 'link_url=:link_url' )
			->bindValue( 'link_url', $url, ptSTR )
			->exec()
			->fetchColumn(0);
	}
	//------------------------------------------------------------------------
	/**
	*
	*/
	public function insert( $ds )
	{
		return query::factory()
			->insert_into( 'links' )
			->pt( ptSTR )
			->insert_fields( array( 'link_url', 'link_desc', 'link_page_title' ) )
			->autoBind( $ds )
			->exec()
			->lastInsertId;
	}
	//------------------------------------------------------------------------
	/**
	*
	*/
	public function urlExistsAndUpdate( $url )
	{
		$rows = query::factory()
			->select()
			->from( 'links' )
			->where( 'link_url=:url' )
			->bindValue( 'url', $url, ptSTR )
			->exec()
			->fetchAll();
			
		if ( $rows == false )
		{
			return linkNotExists;
		}

		$link = $rows[0];

		if ( time() <= ( strtotime( $link->link_ts )+3600 ) )
		{
			return linkExistsTooNew;
		}

		query::factory()
			->update( 'links' )
			->setLiteral( 'link_ts=now()' )
			->setLiteral( 'link_add_count=link_add_count+1' )
			->where( 'link_id = :id' )
			->bindValue( 'id', $link->link_id, ptINT )
			->exec();
			
		query::factory()
			->update( 'links' )
			->setLiteral( 'link_ts=now()' )
			->setLiteral( 'link_spam_report_count=link_spam_report_count-1' )
			->where( 'link_id = :id' )
			->where( 'link_spam_report_count>0')
			->bindValue( 'id', $link->link_id, ptINT )
			->exec();

		return linkExistsUpdated;
	}
	//------------------------------------------------------------------------
	/**
	*
	*/
	public function fetchById( $id )
	{
		return query::factory()
			->select()
			->from( 'links' )
			->where( 'link_id = :link_id' )
			->bindValue( 'link_id', $id, ptINT )
			->exec()
			->fetch_ds();
	}
	//------------------------------------------------------------------------
	/**
	*
	*/
	public function getFetchQuery( $user = null )
	{
		$q = tgsfPaginateQuery::factory()
			->select()
			->from( 'links' )
			->order_by( 'link_ts desc, link_id desc' );
		
		if ( $user !== null )
		{
			$q
			->where( 'links_user = :id' )
			->bindValue( 'id', $user, ptINT );
		}

		return $q;
	}
	//------------------------------------------------------------------------
	/**
	*
	*/
	public function spam( $id )
	{
		query::factory()
			->update( 'links' )
			->setLiteral( 'link_spam_report_count=link_spam_report_count+1' )
			->where( 'link_id=:id' )
			->bindValue( 'id', $id, ptINT )
			->exec();

		return query::factory()
			->delete_from( 'links' )
			->where( 'link_id = :id' )
			->bindValue( 'id', $id, ptINT )
			->and_where( 'link_spam_report_count > ' . config( 'max_spam_reports' ) )
			->exec()
			->rowCount > 0;
	}
	//------------------------------------------------------------------------
	/**
	*
	*/
	public function addTagsToLink( $url, $_tags )
	{
		$tagModel = load_model( 'tag' );
		$linkTagModel = load_model( 'link_tag' );

		$ds = dsFactory::ds();
		$ds->setVar( 'link_tag_link_id', $this->getId( $url ) );

		$tags = explode( ',', $_tags );

		array_map( 'trim', $tags );

		foreach( $tags as $tag )
		{
			$tag = trim( $tag );
			$tag_id = $tagModel->insert( dsFactory::ds()->setVar( 'tag_text', $tag ) );

			$ds->setVar( 'link_tag_tag_id', $tag_id );
			$linkTagModel->insert( $ds );
		}
	}
}

return new links_model();
